package com.example.yam.greenapp;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;

import java.util.List;

/**
 * Created by 杨建利 on 2017/2/3.
 */

public class DialogGoodsAdapter extends ArrayAdapter<DGoods> {
    private int resourceId;
    private String TAG = "DialogGoodsAdapter";
    public DialogGoodsAdapter(Context context, int textViewResourceId, List objects) {
        super(context,textViewResourceId, objects);
        this.resourceId=textViewResourceId;
    }
    @NonNull
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        DGoods good=getItem(position);
        View view;
        ViewHoder viewHoder;
        //创建ImageLoader,用于将图片存入缓存和从缓存中取出图片
        ImageLoader mImageLoader= MyApplication.getmImageLoader();
        if(convertView == null) {//如果没有进行过布局

            view = LayoutInflater.from(getContext()).inflate(resourceId, null);

            viewHoder = new ViewHoder();

            viewHoder.imageid = (ImageView) view.findViewById(R.id.dialog_list_pic);
            viewHoder.name = (TextView) view.findViewById(R.id.dialog_list_gname);
            viewHoder.danwei = (TextView) view.findViewById(R.id.dialog_list_danwei);
            viewHoder.price = (TextView) view.findViewById(R.id.dialog_list_price);
            viewHoder.num = (TextView) view.findViewById(R.id.dialog_list_num);
            viewHoder.total = (TextView) view.findViewById(R.id.dialog_list_total);
            viewHoder.cancle = (TextView) view.findViewById(R.id.dialog_list_cancel);

            viewHoder.cancle.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    //Log.v(TAG,MyApplication.dialoggoodsList.get(position).getTotal());
                    MyApplication.diaSum -= Float.valueOf(MyApplication.dialoggoodsList.get(position).getTotal().replace("元",""));
                    MyApplication.dialoggoodsList.remove(position);
                    notifyDataSetChanged();
                }
            });
            //将viewHoder存储在view的Tag中
            view.setTag(viewHoder);
        }else {
            view = convertView;
            viewHoder = (ViewHoder) view.getTag();//重新获取viewhoder
        }
        /*参考自http://www.cnblogs.com/jerehedu/p/4602151.html*/
        //网络路径 以及监听器-从内存中调入，没有则申请网络
        Log.v("测试",good.getImageid());
        mImageLoader.get(good.getImageid(),ImageLoader.getImageListener(viewHoder.imageid,R.drawable.bicon, R.drawable.bicon));
        viewHoder.name.setText(good.getName());
        viewHoder.danwei.setText("/"+good.getDanwei());
        viewHoder.price.setText(good.getPrice()+"元");
        viewHoder.num.setText("数量:"+good.getNum());
        viewHoder.total.setText("小计:"+good.getTotal()+"元");
        return view;
    }
    class ViewHoder{//缓存控件
        ImageView imageid;//图片
        TextView name;//名称
        TextView danwei;//单位
        TextView price;//价格
        TextView num;//数量
        TextView total;//总价
        TextView cancle;//删除
    }

}
